package com.rabbiter.oes.controller;

import com.rabbiter.oes.dto.LoginDTO;
import com.rabbiter.oes.dto.RePassDTO;
import com.rabbiter.oes.dto.RegisterDTO;
import com.rabbiter.oes.entity.Admin;
import com.rabbiter.oes.entity.Student;
import com.rabbiter.oes.entity.Teacher;
import com.rabbiter.oes.enums.RoleEnum;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.LoginServiceImpl;
import com.ramostear.captcha.HappyCaptcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "登录")
@RestController
public class LoginController {

    @Autowired
    private LoginServiceImpl loginService;

    @ApiOperation(value = "登录", notes = "登录", httpMethod = "POST")
    @PostMapping("/login")
    public R login(@RequestBody LoginDTO login, HttpServletRequest request,HttpServletResponse response) {

        String username = login.getUsername();
        String password = login.getPassword();
        // 验证码
        String captcha = login.getCaptcha();
        boolean flag = HappyCaptcha.verification(request,captcha,true);
        if (!flag) {
            return R.error("验证码错误");
        }
        RoleEnum roleEnum = loginService.getRole(username);
        if (roleEnum.getCode() == 0) {
            Admin adminRes = loginService.adminLogin(username, password);
            if (adminRes != null) {
                Cookie token = new Cookie("rb_token", adminRes.getCardId());
                token.setPath("/");
                Cookie role = new Cookie("rb_role", "0");
                role.setPath("/");

                //将cookie对象加入response响应
                response.addCookie(token);
                response.addCookie(role);

                return R.ok("请求成功").data(adminRes);
            }
        }else if (roleEnum.getCode() == 1) {
            Teacher teacherRes = loginService.teacherLogin(username, password);
            if (teacherRes != null) {
                Cookie token = new Cookie("rb_token", teacherRes.getCardId());
                token.setPath("/");
                Cookie role = new Cookie("rb_role", "1");
                role.setPath("/");
                response.addCookie(token);
                response.addCookie(role);
                return R.ok("请求成功").data(teacherRes);
            }
        }else if (roleEnum.getCode() == 2) {
            Student studentRes = loginService.studentLogin(username, password);
            if (studentRes != null) {
                Cookie token = new Cookie("rb_token", studentRes.getCardId());
                token.setPath("/");
                Cookie role = new Cookie("rb_role", "2");
                role.setPath("/");
                response.addCookie(token);
                response.addCookie(role);
                return R.ok("请求成功").data(studentRes);
            }
        }

        return R.logicError("登录失败，账号或密码错误！");
    }

    @ApiOperation(value = "找回密码", notes = "找回密码", httpMethod = "POST")
    @PostMapping("/repass")
    public R repass(@RequestBody RePassDTO rePassDTO) {
        return loginService.repass(rePassDTO);
    }

    @ApiOperation(value = "注册", notes = "注册", httpMethod = "POST")
    @PostMapping("/register")
    public R register(@RequestBody RegisterDTO registerDTO) {
        return loginService.register(registerDTO);
    }

    @ApiOperation(value = "退出登录", notes = "退出登录", httpMethod = "POST")
    @PostMapping("/logout")
    public R logout(HttpServletResponse response) {
        Cookie token = new Cookie("rb_token", null);
        token.setPath("/");
        token.setMaxAge(0);
        Cookie role = new Cookie("rb_role", null);
        role.setPath("/");
        role.setMaxAge(0);
        response.addCookie(token);
        response.addCookie(role);
        return R.ok("退出登录成功");
    }

    @ApiOperation(value = "获取验证码", notes = "获取验证码", httpMethod = "GET")
    @GetMapping("/captcha")
    public void happyCaptcha(HttpServletRequest request, HttpServletResponse response){
        HappyCaptcha.require(request,response).build().finish();
    }


}
